#include "Game.h"
#include "ModeCreateJoin.h"
#include "ModeLobby.h"
#include "ModeGame.h"

using namespace GTL;
using namespace GTL::Engine;

int SummonerGame::StartGame()
{
	SetNext('conn');

	net = (NetworkService*)GetService("Network");

	iTiles = Backend->LoadImage("Data/tiles.png", true);

	IsServer = false;
	MaxPlayers = 2;

	return 0;
}

Mode* SummonerGame::CreateMode( int id, int arg1, int arg2 )
{
	switch (id)
	{
	case 'conn':
		return AutoMembers::Producible::Produce<ModeCreateJoin>();
	case 'lobb':
		return AutoMembers::Producible::Produce<ModeLobby>();
	case 'game':
		return AutoMembers::Producible::Produce<ModeGame>();
	}
	return 0;
}

void SummonerGame::ServerInit(uint16_t port)
{
	net->StartServer(port);

	IsServer = true;
}

void SummonerGame::ClientInit(const char* ip, uint16_t port)
{
	net->StartConnectingToServer(ip, port);

	IsServer = false;
}

SummonerGame::Player* SummonerGame::FindPlayer( int id )
{
	for (size_t i=0; i<Players.size(); i++)
		if (Players[i].ID == id)
			return &Players[i];
	return 0;
}